package com.fl.springcloud.controller;

import com.fl.springcloud.entities.CommonResult;
import com.fl.springcloud.entities.PayMent;
import com.fl.springcloud.service.PaymentFeignService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
@DefaultProperties(defaultFallback = "payment_fallback_Global") //默认降级方法！
public class PaymentFeignController {
    @Resource
    private PaymentFeignService service;

    @GetMapping(value = "/consumer/getPayment/{id}")
    public CommonResult<PayMent> getPaymentById(@PathVariable("id") Long id){

        CommonResult<PayMent> payment = service.getPayment(id);
        if (payment.getCode()==200){
            return payment;
        }else {
            return new CommonResult<>(500,"查询出错！！");
        }
    }

    @GetMapping(value = "/consumer/payment/feign/timeout")
    public String PaymentFeignTimeout(){
        return service.PaymentFeignTimeout();
    }


    @GetMapping(value = "/consumer/payment/hystrix/ok/{id}")
    public String payment_ok(@PathVariable("id") Integer id)
    {
        return service.payment_ok(id);
    }

    @GetMapping(value = "/consumer/payment/hystrix/timeout/{id}")
    @HystrixCommand(fallbackMethod = "timeoutHandler",commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "2500")
    })
//    @HystrixCommand
    public String payment_timeout(@PathVariable("id") Integer id){
        String timeout = service.payment_timeout(id);
        return timeout;
    }

    public String timeoutHandler(@PathVariable("id") Integer id){
        return "我是消费者80=>"+"服务提供者=>8001系統超時或错误! /(ㄒoㄒ)/~~ id=> "+id;
    }

    public String payment_fallback_Global(){
        return "程序运行出错";
    }

    @GetMapping(value = "consumer/payment/zipkin")
    public void zipkin(){
        service.zipkin();
    }
}
